var express = require('express');
var router = express.Router();

const user = require("../controller/user");
const auth = require("../controller/auth");

/**
 * @openapi
 * /users/register:
 *   post:
 *     tags:
 *       - Users
 *     summary: 注册一个新用户
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               username:
 *                 type: string
 *               truename:
 *                 type: string
 *               phoneNumber:
 *                 type: string
 *               password:
 *                 type: string
 *     responses:
 *       200:
 *         description: User registered successfully
 *       400:
 *         description: Bad request
 */
router.post("/register", user.register);
/**
 * @openapi
 * /users/login:
 *   post:
 *     tags:
 *       - Users
 *     summary: 登录   
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               phoneNumber:
 *                 type: string
 *               password:
 *                 type: string
 *     responses:
 *       200:
 *         description: 用户登录成功
 *       400:
 *         description: Bad request
 */
router.post("/login", user.login);   
/**
 * @openapi
 * /users/logout:
 *   get:
 *     tags:
 *       - Users
 *     summary: 用户退出登录
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 用户成功退出系统
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   description: 退出成功的消息
 *                 logoutStatus:
 *                   type: string
 *                   description: 退出状态，成功时为"success"
 *       400:
 *         description: 非正确访问路径或未登录用户尝试退出
 */
router.get("/logout", auth.isLogin, user.logout);
/**
 * @openapi
 * /users/search:
 *   get:
 *     tags:
 *       - Users
 *     summary: 按关键字搜索用户信息
 *     description: 此接口允许管理员根据关键字搜索用户信息。可以指定结果数量限制和偏移量。
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: q
 *         required: true
 *         description: 搜索关键字
 *         schema:
 *           type: string
 *       - in: query
 *         name: limit
 *         description: 结果数量限制。默认值为-1，表示不限制数量。
 *         schema:
 *           type: integer
 *       - in: query
 *         name: offset
 *         description: 结果偏移量。默认值为-1，表示不偏移。
 *         schema:
 *           type: integer
 *     responses:
 *       200:
 *         description: 获取指定关键字的用户信息列表
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   description: 操作成功的消息
 *                 data:
 *                   type: array
 *                   items:
 *                     type: object
 *                     properties:
 *                       id:
 *                         type: string
 *                         description: 用户ID
 *                       username:
 *                         type: string
 *                         description: 用户名
 *       401:
 *         description: 未授权访问，只有登录的管理员可以访问此接口
 *       400:
 *         description: 请求格式错误或数据不完整
 */
router.get("/search", auth.isLogin, auth.isAdmin, user.search);
/**
 * @openapi
 * /users/count:
 *   get:
 *     tags:
 *       - Users
 *     summary: 获取用户总数
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 成功获取用户总数
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   description: 操作成功的消息
 *                 data:
 *                   type: integer
 *                   description: 用户总数
 *       401:
 *         description: 未授权访问，只有管理员可以访问此接口
 */
router.get("/count", auth.isAdmin, user.getCount);
/**
 * @openapi
 * /users/change-info:
 *   patch:
 *     tags:
 *       - Users
 *     summary: 修改个人信息
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               nickname:
 *                 type: string
 *                 description: 昵称
 *               truename:
 *                 type: string
 *                 description: 真实姓名
 *               avatar:
 *                 type: string
 *                 description: 头像URL
 *     responses:
 *       200:
 *         description: 个人信息修改成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   description: 操作成功的消息
 *                 changes:
 *                   type: integer
 *                   description: 受影响的行数
 *       401:
 *         description: 未授权访问，只有登录的用户可以访问此接口
 *       400:
 *         description: 请求格式错误或数据不完整
 */
router.patch("/change-info", auth.isLogin, user.changeInfo);
/**
 * @openapi
 * /users/change-password:
 *   patch:
 *     tags:
 *       - Users
 *     summary: 修改个人密码
 *     description: 此接口允许用户修改自己的密码。
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               password:
 *                 type: string
 *                 description: 新密码，需要进行加密处理
 *     responses:
 *       200:
 *         description: 个人密码修改成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   description: 操作成功的消息
 *                 changes:
 *                   type: integer
 *                   description: 受影响的行数
 *       401:
 *         description: 未授权访问，只有登录的用户可以访问此接口
 *       400:
 *         description: 请求格式错误或数据不完整
 */
router.patch("/change-password", auth.isLogin, user.changePassword);
/**
 * @openapi
 * /users/reset-password/{id}:
 *   patch:
 *     tags:
 *       - Users
 *     summary: 重置个人密码
 *     description: 此接口允许管理员重置用户的密码。
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         description: 用户ID
 *         schema:
 *           type: string
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               password:
 *                 type: string
 *                 description: 新密码，需要进行加密处理
 *     responses:
 *       200:
 *         description: 个人密码重置成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   description: 操作成功的消息
 *                 changes:
 *                   type: integer
 *                   description: 受影响的行数
 *       401:
 *         description: 未授权访问，只有登录的管理员可以访问此接口
 *       400:
 *         description: 请求格式错误或数据不完整
 */
router.patch("/reset-password/:id", auth.isLogin, auth.isAdmin, user.resetPassword);
/**
 * @openapi
 * /users/findByPhone/{id}:
 *   get:
 *     tags:
 *       - Users
 *     summary: 通过电话号码查找用户信息
 *     description: 根据指定的电话号码获取用户详细信息。此操作需要用户已登录且具备管理员权限。
 *     security:
 *       - bearerAuth: []   
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         description: 用户的电话号码，用于查询
 *         schema:
 *           type: string
 *     responses:
 *       200:
 *         description: 用户信息获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   example: 200
 *                   description: 状态码，200表示成功
 *                 message:
 *                   type: string
 *                   example: 获取用户信息【手机号】成功
 *                   description: 操作结果消息
 *                 data:
 *                   $ref: '#/components/schemas/User'
 *       401:
 *         description: 未授权访问，用户未登录或非管理员
 *       404:
 *         description: 找不到指定电话号码的用户
 *       500:
 *         description: 服务器内部错误
 * components:
 *   schemas:
 *     User:
 *       type: object
 *       properties:
 *         id:
 *           type: string
 *           description: 用户ID
 *         phoneNumber:
 *           type: string
 *           description: 用户电话号码
 */
router.get("/findByPhone/:id", auth.isLogin, auth.isAdmin, user.findByPhone);
/**
 * @openapi
 * /users/{id}:
 *   get:
 *     tags:
 *       - Users
 *     summary: 获取用户信息
 *     description: 此接口允许用户获取特定ID的用户信息。
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         description: 用户ID
 *         schema:
 *           type: string
 *     responses:
 *       200:
 *         description: 获取用户信息成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   description: 操作成功的消息
 *                 data:
 *                   type: object
 *                   properties:
 *                     // 用户信息字段...
 *       401:
 *         description: 未授权访问，只有登录的用户可以访问此接口
 *       404:
 *         description: 用户ID不存在
 */
router.get("/:id", auth.isLogin, user.find);

/**
 * @openapi
 * /users:
 *   get:
 *     tags:
 *       - Users
 *     summary: 获取用户列表信息
 *     description: 此接口允许管理员获取用户列表。可以指定结果数量限制和偏移量。
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: limit
 *         description: 结果数量限制。默认值为-1，表示不限制数量。
 *         schema:
 *           type: integer
 *       - in: query
 *         name: offset
 *         description: 结果偏移量。默认值为-1，表示不偏移。
 *         schema:
 *           type: integer
 *     responses:
 *       200:
 *         description: 获取用户信息列表成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   description: 操作成功的消息
 *                 data:
 *                   type: array
 *                   items:
 *                     type: object
 *                     properties:
 *                       // 用户信息字段...
 *       401:
 *         description: 未授权访问，只有登录的管理员可以访问此接口
 *       400:
 *         description: 请求格式错误或数据不完整
 */
router.get("/", auth.isLogin, auth.isAdmin, user.findAll);
/**
 * @openapi
 * /users:
 *   post:
 *     tags:
 *       - Users
 *     summary: 新增用户信息
 *     description: 此接口允许管理员添加新的用户信息。
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               username:
 *                 type: string
 *                 description: 用户名
 *               password:
 *                 type: string
 *                 description: 密码，需加密处理
 *               nickname:
 *                 type: string
 *                 description: 昵称
 *               truename:
 *                 type: string
 *                 description: 真实姓名
 *               avatar:
 *                 type: string
 *                 description: 头像URL
 *               role:
 *                 type: string
 *                 description: 角色
 *               email:
 *                 type: string
 *                 description: 电子邮箱
 *               phoneNumber:
 *                 type: string
 *                 description: 电话号码
 *               isVerified:
 *                 type: boolean
 *                 description: 是否验证
 *               twoFactorEnabled:
 *                 type: boolean
 *                 description: 是否启用两因素认证
 *               preferredLanguage:
 *                 type: string
 *                 description: 首选语言，可选
 *               timezone:
 *                 type: string
 *                 description: 时区，可选
 *     responses:
 *       201:
 *         description: 用户信息新增成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   description: 操作成功的消息
 *                 lastID:
 *                   type: integer
 *                   description: 新增用户的ID
 *       401:
 *         description: 未授权访问，只有登录的管理员可以访问此接口
 *       400:
 *         description: 请求格式错误或数据不完整
 */
router.post("/", auth.isLogin, auth.isAdmin, user.add);
/**
 * @openapi
 * /users/{id}:
 *   put:
 *     tags:
 *       - Users
 *     summary: 修改用户信息
 *     description: 此接口允许管理员修改特定ID的用户信息。
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         description: 用户ID
 *         schema:
 *           type: string
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               username:
 *                 type: string
 *                 description: 用户名
 *               password:
 *                 type: string
 *                 description: 密码，需加密处理
 *               nickname:
 *                 type: string
 *                 description: 昵称
 *               truename:
 *                 type: string
 *                 description: 真实姓名
 *               avatar:
 *                 type: string
 *                 description: 头像URL
 *               role:
 *                 type: string
 *                 description: 角色
 *               email:
 *                 type: string
 *                 description: 电子邮箱
 *               phone_number:
 *                 type: string
 *                 description: 电话号码
 *               is_verified:
 *                 type: boolean
 *                 description: 是否验证
 *               two_factor_enabled:
 *                 type: boolean
 *                 description: 是否启用两因素认证
 *               account_status:
 *                 type: string
 *                 description: 账户状态
 *     responses:
 *       200:
 *         description: 用户信息更新成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   description: 操作成功的消息
 *                 changes:
 *                   type: integer
 *                   description: 受影响的行数
 *       401:
 *         description: 未授权访问，只有登录的管理员可以访问此接口
 *       404:
 *         description: 用户ID不存在
 */
router.put("/:id", auth.isLogin, auth.isAdmin, user.update);
/**
 * @openapi
 * /users/{id}:
 *   delete:
 *     tags:
 *       - Users
 *     summary: 删除用户信息
 *     description: 此接口允许管理员删除特定ID的用户信息。
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         description: 用户ID
 *         schema:
 *           type: string
 *     responses:
 *       204:
 *         description: 用户信息删除成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   description: 操作成功的消息
 *                 changes:
 *                   type: integer
 *                   description: 受影响的行数
 *       401:
 *         description: 未授权访问，只有登录的管理员可以访问此接口
 *       404:
 *         description: 用户ID不存在
 */
router.delete("/:id", auth.isLogin, auth.isAdmin, user.remove);

router.get('/users', function(req, res, next) {
  res.json({ msg: "用户路由响应"});
});

module.exports = router;